// -*- c++ -*-
/**************************************************************/
/*  This file is licensed from, and is a trade secret of:     */
/*                                                            */
/*    Mercury Computer Systems, Incorporated                  */
/*    199 Riverneck Road                                      */
/*    Chelmsford, Massachusetts 01824-2820                    */
/*    United States of America                                */
/*    Telephone + 1 978 256-1300                              */
/*                                                            */
/*  Refer to your Software License Agreement for provisions   */
/*  on use, duplication, or disclosure.                       */
/*  US. Government users and prime contractors are also       */
/*  governed by the following Data Rights Notice:             */
/*  Use, duplication or disclosure by the US. Government is   */
/*  subject to the provisions set forth in FAR 52.227-14 for  */
/*  restricted rights or other agency data rights provisions  */
/*  on commercial software and documentation, as applicable.  */
/*                                                            */
/*  This file: (a) was developed at private expense, and no   */
/*  part was developed with government funds, (b) is a        */
/*  trade secret of Mercury Computer Systems, Inc. for the    */
/*  purposes of the Freedom of Information Act, (c) is        */
/*  "commercial computer software" subject to limited         */
/*  utilization as provided in the above noted Software       */
/*  License Agreement and (d) is in all respects data         */
/*  belonging exclusively to Mercury Computer Systems, Inc..  */
/*                                                            */
/**************************************************************/

/******************************************************************/
/* Copyright (c) 1999-2003, Mercury Computer Systems, Inc.,       */
/* Chelmsford MA.,and all third party embedded software sources.  */
/* All rights reserved under the Copyright laws of US. and        */
/* international treaties.                                        */
/*                                                                */
/* Patent Pending                                                 */
/******************************************************************/


//$COPYRIGHT_END


#ifndef MAC_PORT_H__
#define MAC_PORT_H__

#include "CF__Port_impl.h"
#include "macS.h"
#include "mac.h"
#include "mc_error.h"

/*
 * ----------------------------------------------------------------------
 * Input ports
 * ----------------------------------------------------------------------
 */

// MAC Local Xmit Error Control Input Port template class.
template<typename T>
class MAC_EXPORT MAC_ErrCtrl_XmitInPort_impl
  : public virtual POA_SDR_FM3TR::MAC_Signaling::MAC_ErrorControl_XmitLocal
{
public:
  MAC_ErrCtrl_XmitInPort_impl (T * parent)
    : m_parent (parent)
  {
  }

  virtual ~MAC_ErrCtrl_XmitInPort_impl ()
  {
  }

  CORBA::Object_ptr
  getPort ()
  {
    return _this ();
  }


  virtual
  void
  signalNAK (SDR_FM3TR::MAC_Signaling::PacketID error_packet) 
    throw (CORBA::SystemException)
  {
    m_parent->signalNAK (error_packet);
  }

private:
  T * m_parent;
};

// MAC Receive Local Error Control Input Port template class.
template<typename T>
class MAC_EXPORT MAC_ErrCtrl_ReceiveInPort_impl
  : public virtual POA_SDR_FM3TR::MAC_Signaling::MAC_ErrorControl_ReceiveLocal
{
public:
  MAC_ErrCtrl_ReceiveInPort_impl (T * parent)
    : m_parent (parent)
  {
  }

  virtual ~MAC_ErrCtrl_ReceiveInPort_impl ()
  {
  }

  CORBA::Object_ptr
  getPort ()
  {
    return _this ();
  }

  virtual
  void
  signalACK ()
    throw (CORBA::SystemException)
  {
    m_parent->signalACK ();
  }

  virtual
  void
  signalNAK()
    throw (CORBA::SystemException)
  {
    m_parent->signalNAK ();
  }

private:
  T * m_parent;
};

// MAC Local Flow Control Input Port template class.
template<typename T>
class MAC_EXPORT MAC_FlowCtrl_InPort_impl
  : public virtual POA_SDR_FM3TR::MAC_Signaling::MAC_FlowControl_Local
{
public:
  MAC_FlowCtrl_InPort_impl (T * parent)
    : m_parent (parent)
  {
  }

  virtual ~MAC_FlowCtrl_InPort_impl ()
  {
  }

  CORBA::Object_ptr
  getPort ()
  {
    return _this ();
  }

  // From POA_SDR_FM3TR::MAC_Signaling::MAC_FlowControl_Local.
  virtual
  void
  signalHighWatermark ()
    throw (CORBA::SystemException)
  {
    m_parent->signalHighWatermark ();
  }

  virtual
  void
  signalLowWatermark ()
    throw (CORBA::SystemException)
  {
    m_parent->signalLowWatermark ();
  }

  virtual
  void
  signalEmpty() 
    throw (CORBA::SystemException)
  {
    m_parent->signalEmpty ();
  }

private:
  T * m_parent;
};

// MAC Feedback Input Port template class.
template<typename T>
class MAC_EXPORT MAC_SendFeedback_InPort_impl
  : public virtual POA_SDR_FM3TR::MAC_Signaling::MAC_Signal_Remote
{
public:
  MAC_SendFeedback_InPort_impl (T * parent)
    : m_parent (parent)
  {
  }

  virtual ~MAC_SendFeedback_InPort_impl ()
  {
  }

  CORBA::Object_ptr
  getPort ()
  {
    return _this ();
  }

  virtual
  void
  SendFeedback (SDR_FM3TR::MAC_Signaling::MAC_Signal_Ack ack, 
		SDR_FM3TR::MAC_Signaling::MAC_Signal_FlowControl fc)
    throw (CORBA::SystemException)
  {
    m_parent->SendFeedback (ack, fc);
  }

private:
  T * m_parent;
};

// LLC Control Input Port template class.
template<typename T>
class MAC_EXPORT LLC_Ctrl_InPort_impl
  : public virtual POA_SDR_FM3TR::LLC_Control
{
public:
  LLC_Ctrl_InPort_impl (T * parent)
    : m_parent (parent)
  {
  }

  virtual ~LLC_Ctrl_InPort_impl ()
  {
  }

  CORBA::Object_ptr
  getPort ()
  {
    return _this ();
  }


  virtual
  void
  startStream ()
    throw (CORBA::SystemException)
  {
    m_parent->startStream ();
  }

  virtual
  void
  stopStream ()
    throw (CORBA::SystemException)
  {
    m_parent->stopStream ();
  }

private:
  T * m_parent;
};

/*
 * ----------------------------------------------------------------------
 * Output ports
 * ----------------------------------------------------------------------
 */

template<typename T>
class MAC_EXPORT MAC_Ctrl_OutPort_impl
  : public CF__Port_impl
{
public:
  MAC_Ctrl_OutPort_impl ()
    : m_already_connected (false)
  {
    m_remote_port = T::_nil();
  }

  virtual ~MAC_Ctrl_OutPort_impl ()
  {
    CORBA::release (m_remote_port);
  }

  CORBA::Object_ptr
  getPort ()
  {
    return _this ();
  }

  virtual
  void
  connectPort (CORBA::Object_ptr connecting_port,
	       const CORBA::Char * connectionId)
    throw (CORBA::SystemException,
	   CF::Port::InvalidPort, 
           CF::Port::OccupiedPort)
  {
    if (m_already_connected) {
      throw CF::Port::OccupiedPort ();
    }

    try {
      m_remote_port = T::_narrow (connecting_port);
    }
    catch ( ... ) {
      throw CF::Port::InvalidPort ();
    }

    if (CORBA::is_nil (m_remote_port)) {
      throw CF::Port::InvalidPort ();
    }

    m_already_connected = true;
  }  // End of ::connectPort(... )

  T *
  getRemotePort ()
  {
    return m_remote_port;
  }

  bool
  IsConnected ()
  {
    return m_already_connected;
  }

private:
  T * m_remote_port;
  bool m_already_connected;
};

#endif
